#include <func.h>

//客户端
int main(void)
{
    char file_name[10] = "1.txt";
    int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(sock_fd == -1)
        error(1, errno, "socket");

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi("8080"));
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if(connect(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
        error(1, errno, "connect");


    int fd = open(file_name, O_RDONLY);
    if(fd == -1)
        error(1, errno, "open");

    char buf[128];
    while(1)
    {
        if(send(sock_fd, file_name, strlen(file_name), 0) == -1)
            error(1, errno, "send");
        char receive[50];
        int count = recv(sock_fd, receive, 50, 0);
        if(strncmp(receive, "true", 5) == 0)
        {
            ssize_t count = read(fd, buf, 128);
            if(count == -1)
                error(1, errno, "read");
            else if(count == 0)
                break;
            else
            {
                if(send(sock_fd, buf, count, 0) == -1)
                    error(1, errno, "send");
            }
        }

    }

    close(fd);
    close(sock_fd);

    return 0;
}

